<?php

require_once dirname(__FILE__) . '/../lib/operacionesGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/operacionesGeneratorHelper.class.php';

/**
 * operaciones actions.
 *
 * @package    tercermilenio
 * @subpackage operaciones
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class operacionesActions extends autoOperacionesActions {

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            $tipoOperacion = intval($form->getValue('op_tipo'));
            $montoOperacion = $form->getValue('op_monto');

            //obtener monto actual de caja
            $caja = CajaPeer::ObtenerUltima();

            //calcular monto
            if (!empty($caja)) {
                $montoCaja = $caja->getCjaMonto();
            } else {
                $montoCaja = 0;
            }

            //comprobar operacion y existencia en caja mayor a monto operacion
            if ($tipoOperacion == 1) {
                $monto = $montoCaja + $montoOperacion;
            } else {
                if ($montoCaja >= $montoOperacion) {
                    $monto = $montoCaja - $montoOperacion;
                } else {
                    $opErronea = true;
                }
            }

            if (!isset($opErronea)) {

                $operacion = new Operaciones();
                $operacion->setOpMonto($form->getValue('op_monto'));
                $operacion->setOpMotivo($form->getValue('op_motivo'));
                $operacion->setOpTipo($form->getValue('op_tipo'));
                $operacion->setOpUsuario(sfContext::getInstance()->getUser()->getGuardUser()->getId());
                $operaciones = $operacion->save();

                $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $operaciones)));

                //obtener la operacion guardada
                $operacion = OperacionesPeer::ObtenerUltima();

                //actualizar caja
                $caja = new Caja();
                $caja->updateCaja($monto, $operacion);

                $this->getUser()->setFlash('notice', $notice);
                $this->redirect('@operaciones');
            } else {
                $this->getUser()->setFlash('error', 'El monto ingresado es mayor al existente en caja.', false);
            }
        } else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = $this->configuration->getForm();
        $this->formCajaDiaria = new cajaDiariaForm();
        $this->operaciones = $this->form->getObject();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->form = $this->configuration->getForm();
        $this->operaciones = $this->form->getObject();
        $this->formCajaDiaria = new cajaDiariaForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->operaciones = $this->getRoute()->getObject();
        $this->form = $this->configuration->getForm($this->operaciones);
        $this->formCajaDiaria = new cajaDiariaForm();
    }

    public function executeGetCantidadFinal(sfWebRequest $request) {
        $this->formCaja = new cajaDiariaForm();
        if ($request->hasParameter('caja')) {
            $valor = $request->getParameter('caja');
        }
        $cantidad_existente = OperacionesPeer::obtenerCajaExistenteDelDia();
        if (isset($valor)) {

            $this->cantidad_final = $cantidad_existente - $valor;
        } else {
            $this->cantidad_final = $cantidad_existente;
        }

        $ws = $this->formCaja->getWidgetSchema();
        //$ws->seAttribute();
        $this->formCaja->setDefault('cant_final', $this->cantidad_final);
        $this->formCaja->setWidgetSchema($ws);
    }

}
